package com.lt.flashcard.view;

import org.apache.commons.lang3.StringUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Cart;
import com.lt.flashcard.model.service.EcommerceService;
import com.lt.flashcard.model.service.Repository;

public class CartVm {

	@Init
	public void init() {
	}
	
	/**
	 * Get the card
	 * @return
	 */
	public Cart getCart() {
		EcommerceService srv = Repository.getInstance().getEcommerceService();
		return srv.getCurrentCart();
	}
	
	@NotifyChange("cart")
	@Command
	public void delete(@BindingParam("proid") Long proid) {
		getCart().remove(proid);
	}
	
	@Command
	public void search(@BindingParam("search") String search) {
		if(StringUtils.isNotBlank(search)) {
			String url = Utility.removeContextPath(Utility.generateUrl("store", null, "search=" + search));
			Utility.redirect(url, true);
		}
	}
}
